package com.heima.controller;

import com.heima.pojo.AlertValue;
import com.heima.pojo.Task;
import com.heima.pojo.TaskType;
import com.heima.pojo.dto.CreTaskDto;
import com.heima.pojo.dto.TaskPageDto;
import com.heima.pojo.vo.*;
import com.heima.service.TaskService;
import com.heima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/api/task-service/task")
public class TaskController {

    @Autowired
    private TaskService taskService;
    @Autowired
    private UserService userService;

    /**
     * 工单统计
     */
    @GetMapping("/taskReportInfo/{start}/{end}")
    public List<TaskReportInfoVo> taskReportInfo(@PathVariable String start,@PathVariable String end){
        log.info("start{},end{}",start,end);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date first = null;
        Date last = null;
        try {
            first = simpleDateFormat.parse(start);
            last = simpleDateFormat.parse(end);
        } catch (ParseException e) {
            log.info("日期信息转换失败" ,e);
        }

        List<TaskReportInfoVo> vos = taskService.taskReportInfo(first,last);
        return vos;
    }

    /**
     * 创建工单
     */
    @PostMapping("/create")
    public Boolean createTask(CreTaskDto dto){
        log.info("开始创建工单:{}",dto);
        taskService.createTask(dto);
        return true;
    }

    /**
     * 取消工单
     */
    @PostMapping("/cancel/{taskId}")
    public Boolean cancelTask(@PathVariable Integer taskId , @RequestBody CreTaskDto dto){
        log.info("接收到的taskId:{},dto:{}",taskId,dto );
        taskService.cancelTask(taskId,dto);
        return true;
    }

    /**
     * 工单状态统计
     */
    @GetMapping("collectReport/{start}/{end}")
    public List<TaskCountVo> collectReport(@PathVariable String start,@PathVariable String end){
        log.info("start{},end{}",start,end);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date first = null;
        Date last = null;
        try {
            first = simpleDateFormat.parse(start);
            last = simpleDateFormat.parse(end);
        } catch (ParseException e) {
            log.info("日期信息转换失败" ,e);
        }
        List<TaskCountVo> vos = taskService.collectReport(first,last);
        return vos;
    }

    /**
     * 人员排名
     */
    @GetMapping("/userWorkTop10/{start}/{end}/{isRepair}/{regionId}")
    public List<UserWorkCountVo> userWorkTop10(@PathVariable String start,@PathVariable String end,@PathVariable Boolean isRepair,@PathVariable String regionId){
        log.info("start:{},end:{},isRepair:{},regionId:{}",start,end,isRepair,regionId);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date first = null;
        Date last = null;
        try {
            first = simpleDateFormat.parse(start);
            last = simpleDateFormat.parse(end);
        } catch (ParseException e) {
            log.info("日期信息转换失败" ,e);
        }
        return taskService.userWorkTop10(first, last,isRepair,regionId);
    }

    /**
     * 获取用户工作量(工单统计)
     */
    @GetMapping("/userWork")
    public List<UserSearchVo> userWork(String start,String end,String userId){
        log.info("start:{},end:{},userId:{}",start,end,userId);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date first = null;
        Date last = null;
        try {
            first = simpleDateFormat.parse(start);
            last = simpleDateFormat.parse(end);
        } catch (ParseException e) {
            log.info("日期信息转换失败" ,e);
        }
        return userService.userWork(first,last,userId);
    }

    /**
     * 运营工单搜索
     */
    @GetMapping("/search")
    public TaskPageVo pageQueryRepair(TaskPageDto dto){
        log.info("dto:{}",dto);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date first = null;
        Date last = null;
        try {
            if (dto.getStart() != null && dto.getEnd() != null){
                first = simpleDateFormat.parse(dto.getStart());
                last = simpleDateFormat.parse(dto.getEnd());
            }
        } catch (ParseException e) {
            log.info("日期信息转换失败" ,e);
        }
        dto.setFirst(first);
        dto.setLast(last);
        return taskService.pageQueryRepair(dto);
    }

    /**
     * 所有工单状态
     */
    @GetMapping("/allTaskStatus")
    public List<TaskStatusVo> queryAllStatus(){
        return taskService.queryAllStatus();
    }

    /**
     * 获取工单详情
     */
    @GetMapping("taskInfo/{taskId}")
    public Task queryTaskInfo(@PathVariable Integer taskId){
        log.info("taskId:{}",taskId);
        return taskService.queryTaskInfo(taskId);
    }

    /**
     * 获取补货预警值
     */
    @GetMapping("/supplyAlertValue")
    public Integer getSupplyAlertValue(Integer alertValue){
        log.info("value:{}",alertValue);
        return taskService.getSupplyAlertValue(alertValue);
    }

    /**
     * 获取售货机补货次数
     */
    @GetMapping("/supplyCount/{innerCode}/{start}/{end}")
    public Integer getSupplyTimes(@PathVariable String end, @PathVariable String innerCode, @PathVariable String start){
        log.info("end:{},start:{},innerCode:{}",end,start,innerCode);

        return 0;
    }

    @GetMapping("/repairCount/{innerCode}/{start}/{end}")
    public Integer getRepairTimes(@PathVariable String end, @PathVariable String innerCode, @PathVariable String start){
        log.info("end:{},start:{},innerCode:{}",end,start,innerCode);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date first = null;
        Date last = null;
        try {
            first = simpleDateFormat.parse(start);
            last = simpleDateFormat.parse(end);
        } catch (ParseException e) {
            log.info("日期信息转换失败" ,e);
        }
        Integer times = taskService.getRepairTimes(first,last,innerCode);
        return times;
    }
}
